<template>
	<view class='login'>
		<view class='zz dFlex jCenter_aCenter' v-if="!loginStatus">
			<view class='loginToast'>
				<view class='toast'>登录提示</view>
				<view class='content'>登录后才能体验更多功能 是否现在登陆</view>
				<view class='btns dFlex jBetween_aCenter'>
					<view class='btn' @tap="closeLogin">取消</view>
					<button class='btn login' open-type="getPhoneNumber" @getphonenumber.stop="getPhoneNumber">立即登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			loginStatus: {
				type: Boolean,
				default: false,
			},
			code:{
				type:String,
				default:'',
			},
			status:{
				type:Number,
			}
		},
		data() {
			return {
				
			};
		},
		methods: {
			closeLogin() {
				this.$emit('opFlag', true);
			},
			// 一键登录
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					uni.showToast({
						title:'用户拒绝授权',
						icon:"none"
					})
				} else {
					this.tool.getData('/api/weixin/jscode2session',{
						code:this.code
					}).then(res=>{
						if(res){
							this.tool.getData('/api/weixin/programLogin',{
								encrypt: e.detail.encryptedData,
								iv: e.detail.iv,
								session_key: JSON.parse(res).session_key,
								openid: JSON.parse(res).openid,
								source:'0',
								// param: this.tjr_mobile,
								tjr_mobile:uni.getStorageSync('tjr_mobile')?uni.getStorageSync('tjr_mobile'):'',
							}).then(res=>{
								if(res){
									uni.showToast({
										title:'登录成功',
										icon:"none"
									})
									uni.setStorageSync('uToken',res.userinfo.token);
									uni.setStorageSync('uId',res.userinfo.id);
									uni.setStorageSync('user_level',res.userinfo.user_level);
									getApp().globalData.isLogin = true;
									this.closeLogin();
								}
							}).catch(err=>{
								console.log(err);
							})
						}else{
							wx.login({
								success:(info)=>{
									if (info.code) {
										this.tool.getData('/api/weixin/jscode2session',{
											code:info.code
										}).then(res=>{
											if(res){
												this.tool.getData('/api/weixin/programLogin',{
													encrypt: e.detail.encryptedData,
													iv: e.detail.iv,
													session_key: JSON.parse(res).session_key,
													openid: JSON.parse(res).openid,
													source:'0',
													// param: this.tjr_mobile,
													tjr_mobile:uni.getStorageSync('tjr_mobile')?uni.getStorageSync('tjr_mobile'):'',
												}).then(res=>{
													if(res){
														uni.showToast({
															title:'登录成功',
															icon:"none"
														})
														uni.setStorageSync('uToken',res.userinfo.token);
														uni.setStorageSync('uId',res.userinfo.id);
														getApp().globalData.isLogin = true;
														uni.setStorageSync('user_level',res.userinfo.user_level);
														this.closeLogin();
													}
												}).catch(err=>{
													console.log(err);
												})
											}
										})
									}
								}
							})
						}
					}).catch(err=>{
						console.log(err);
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.loginToast {
		width: 85%;
		background: white; 
		border-radius: 10rpx;
		overflow: hidden;
		font-size:14px; 
		color:#333333;
		.toast {
			padding: 24rpx 0;
			text-align: center;
		}

		.content {
			text-align: center;
			font-weight: bold;
			font-size: 16px;
			padding: 30rpx 135rpx;
			line-height: 30px;
			box-sizing: border-box;
		}

		.btns {
			width: 100%;
			border-top: 1px solid #f8f8f8;

			.btn {
				flex: 1;
				text-align: center;
				padding: 24rpx 0;
				color: #999999;
			}

			.login {
				background:$col_active;
				color: white;
				font-size: 14px;
			}
		}
	}
</style>